using System;
using System.Collections.Generic;
using System.Text;

namespace PACKETPARSERMAX20
{
    public class EventManager
    {

        private int mUpdateStatusLevel = 1;
        private bool silentMode = true;
        private bool pause = false;
        private bool logIt = false;

        public delegate void DelegateUpdateProgress(int pTotal, int pCurrent, string pDescription);
        public event DelegateUpdateProgress EventUpdateProgress;

        public delegate void DelegateUpdateStatusLog(string time, string message, bool attention);
        public event DelegateUpdateStatusLog EventUpdateStatusLog;

        public delegate void DelegateUpdateWarning(object pSource);
        public event DelegateUpdateWarning EventUpdateWarning;

        public delegate void DelegateObject_Updated(object pSource);
        public event DelegateObject_Updated EventObject_Updated;

        public delegate void DelegateLoadHex(byte[] pData);
        public event DelegateLoadHex EventLoadHex;

        public delegate void DelegateCurrentPacketIndex(int pIndex);
        public event DelegateCurrentPacketIndex EventCurrentPacketIndex;

        public int UpdateStatusLevel
        {
            get
            {
                return mUpdateStatusLevel;
            }

            set
            {
                mUpdateStatusLevel = value;
            }
        }

        public bool Pause
        {
            get
            {
                return pause;
            }

            set
            {
                pause = value;
            }
        }

        public bool LogIt
        {
            get
            {
                return logIt;
            }

            set
            {
                logIt = value;
            }
        }

        public bool SilentMode
        {
            get
            {
                return silentMode;
            }

            set
            {
                silentMode = value;

                if (silentMode)
                {
                     UpdateStatusLevel = 1;
                }
                else
                {
                     UpdateStatusLevel = 2;
                }
            }
        }

        public void CurrentPacketIndex(int pIndex)
        {
            if (EventCurrentPacketIndex != null) { EventCurrentPacketIndex(pIndex); }
        }

        public void Object_Updated(object pSource)
        {
            if (EventObject_Updated != null) { EventObject_Updated(pSource); }
        }

        public void UpdateWarning(object pSource)
        {
            if (EventUpdateWarning != null) { EventUpdateWarning(pSource); }
        }    
        
        public void LoadHex(byte[] pData)
        {
            if (EventLoadHex != null) { EventLoadHex(pData); }
        }

        public void UpdateProgress(int pTotal, int pCurrent, string pDescription)
        {
            if (EventUpdateProgress != null) { EventUpdateProgress(pTotal, pCurrent, pDescription); }
        }

        public void UpdateStatusLog(string time, string message, bool attention, int pLevel)
        {
            if (mUpdateStatusLevel > pLevel ) { return; }
 
            if (EventUpdateStatusLog != null) { EventUpdateStatusLog(time, message, attention); }
        }

        private object _lockLog = new object();

        public void Log(string pLog)
        {
            lock (_lockLog)
            {
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter("debug.log", true))
                {
                    sw.Write(pLog);
                    sw.Close();
                }
            }
        }
 

    }
}
